<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateUsersTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('users', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_unicode_ci']);
        $table->addColumn('openid', 'string', ['limit' => 100, 'null' => false, 'comment' => '微信openid'])
            ->addColumn('nickname', 'string', ['limit' => 50, 'null' => true, 'comment' => '用户昵称'])
            ->addColumn('avatar', 'string', ['limit' => 255, 'null' => true, 'comment' => '头像地址'])
            ->addColumn('gender', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '性别: 0-未知, 1-男, 2-女'])
            ->addColumn('age', 'integer', ['limit' => 3, 'null' => true, 'comment' => '年龄'])
            ->addColumn('city', 'string', ['limit' => 50, 'null' => true, 'comment' => '城市'])
            ->addColumn('wechat_id', 'string', ['limit' => 50, 'null' => true, 'comment' => '微信号码'])
            ->addColumn('wechat_qrcode', 'string', ['limit' => 255, 'null' => true, 'comment' => '微信二维码'])
            ->addColumn('balance', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '用户余额'])
            ->addColumn('agent_level', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '代理等级: 1-V1, 2-V2, 3-V3'])
            ->addColumn('parent_id', 'integer', ['default' => 0, 'comment' => '上级代理ID, 0表示无上级'])
            ->addColumn('commission', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '累计佣金'])
            ->addColumn('team_commission', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '团队佣金'])
            ->addColumn('team_count', 'integer', ['default' => 0, 'comment' => '团队成员数量'])
            ->addColumn('level_update_time', 'timestamp', ['null' => true, 'comment' => '等级更新时间'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态: 0-禁用, 1-启用'])
            ->addColumn('create_time', 'timestamp', ['null' => true, 'comment' => '创建时间'])
            ->addColumn('update_time', 'timestamp', ['null' => true, 'comment' => '更新时间'])
            ->addIndex(['openid'], ['unique' => true])
            ->create();
    }
} 